package com.linkallcloud.core.domain;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import com.linkallcloud.core.dto.CompareNameValue;
import com.linkallcloud.core.enums.Agree;
import com.linkallcloud.core.enums.FlowStepType;
import com.linkallcloud.core.enums.Status;
import com.linkallcloud.core.lang.Strings;

public class FlowStep extends Domain {
    private static final long serialVersionUID = 3576311261133439112L;

    private int flowType;// com.linkallcloud.core.enums.FlowStepType
    private String caption;// 流程简略标题

    private String objType;// 业务对象类型(Class simple name)
    private String objPropType;// 业务属性类型，比如修改业务对象的某部分属性，比如修改订单的收货地址相关几个属性，可以设置objPropType=billAddr
    private Long objId;// 业务对象id
    private String objUuid;// 业务对象uuid

    private Integer objOldStatus;// 业务对象原状态
    private String objOldStatusCaption;// 业务对象原状态标题说明
    private Integer objNewStatus;// 审批后状态,DeFLowStatus
    private String objNewStatusCaption;
    private String objCompareJson;// 字段对比 业务对象json

    private Long twfId;// 流程ID
    private String twfUuid;// 流程UUID
    private int progress;// 审批进度百分比
    private int op;// 审批结果,DeAgree
    @JSONField(format = "yyyy-MM-dd")
    private Date opDate;// 审批完成日期

    private Long cuserId;// 流程发起人员id
    private String cuserName;// 流程发起人姓名
    private String cuserPhone;// 流程发起人联系方式
    private String cremark;// 流程发起原因

    /* 以下非数据库字段 */
    private boolean autoCreateStartNode;// 自动创建开始节点
    private boolean needTwfFlow;// 是否需要启动审批流程
    private Boolean completeThenDealBz;// 本节点直接完成的时候是否需要调用处理业务逻辑

    public FlowStep() {
        super();
        this.autoCreateStartNode = false;
        this.flowType = FlowStepType.Status.getCode();
    }

    public FlowStep(String caption, String objType, String objPropType, Long objId, String objUuid) {
        this();
        this.caption = caption;
        this.objType = objType;
        this.objPropType = objPropType;
        this.objId = objId;
        this.objUuid = objUuid;
    }

    public FlowStep(String caption, String objType, String objPropType, Long objId, String objUuid, int op) {
        this(caption, objType, objPropType, objId, objUuid);
        this.op = op;
        if (op == Agree.Yes.getCode()) {
            this.opDate = new Date();
            this.progress = 100;
        }
    }

    public FlowStep(String caption, String objType, String objPropType, Long objId, String objUuid, Long twfId,
            String twfUuid) {
        this(caption, objType, objPropType, objId, objUuid);
        this.twfId = twfId;
        this.twfUuid = twfUuid;
    }

    public FlowStep(String caption, String objType, String objPropType, Long objId, String objUuid,
            Integer objOldStatus, Integer objNewStatus) {
        this(caption, objType, objPropType, objId, objUuid, objOldStatus,
                Status.get(objOldStatus) != null ? Status.get(objOldStatus).getMessage() : "", objNewStatus,
                Status.get(objNewStatus) != null ? Status.get(objNewStatus).getMessage() : "");
    }

    public FlowStep(String caption, String objType, String objPropType, Long objId, String objUuid,
            Integer objOldStatus, String objOldStatusCaption, Integer objNewStatus, String objNewStatusCaption) {
        this(caption, objType, objPropType, objId, objUuid);
        this.objOldStatus = objOldStatus;
        this.objOldStatusCaption = objOldStatusCaption;
        this.objNewStatus = objNewStatus;
        this.objNewStatusCaption = objNewStatusCaption;
        this.flowType = FlowStepType.Status.getCode();
    }

    public FlowStep(String caption, String objType, String objPropType, Long objId, String objUuid,
            Integer objOldStatus, Integer objNewStatus, Long twfId, String twfUuid) {
        this(caption, objType, objPropType, objId, objUuid, objOldStatus,
                Status.get(objOldStatus) != null ? Status.get(objOldStatus).getMessage() : "", objNewStatus,
                Status.get(objNewStatus) != null ? Status.get(objNewStatus).getMessage() : "", twfId, twfUuid);
    }

    public FlowStep(String caption, String objType, String objPropType, Long objId, String objUuid,
            Integer objOldStatus, String objOldStatusCaption, Integer objNewStatus, String objNewStatusCaption,
            Long twfId, String twfUuid) {
        this(caption, objType, objPropType, objId, objUuid, twfId, twfUuid);
        this.objOldStatus = objOldStatus;
        this.objOldStatusCaption = objOldStatusCaption;
        this.objNewStatus = objNewStatus;
        this.objNewStatusCaption = objNewStatusCaption;
        this.flowType = FlowStepType.Status.getCode();
    }

    public FlowStep(String caption, String objType, String objPropType, Long objId, String objUuid,
            String objCompareJson) {
        this(caption, objType, objPropType, objId, objUuid);
        this.objCompareJson = objCompareJson;
        this.flowType = FlowStepType.Prop.getCode();
    }

    public FlowStep(String caption, String objType, String objPropType, Long objId, String objUuid,
            String objCompareJson, Long twfId, String twfUuid) {
        this(caption, objType, objPropType, objId, objUuid, twfId, twfUuid);
        this.objCompareJson = objCompareJson;
        this.flowType = FlowStepType.Prop.getCode();
    }

    public void setOperator(Long cuserId, String cuserName, String cuserPhone, String cremark) {
        this.cuserId = cuserId;
        this.cuserName = cuserName;
        this.cuserPhone = cuserPhone;
        this.cremark = cremark;
    }

    public void complete() {
        this.op = Agree.Yes.getCode();
        this.opDate = new Date();
        this.progress = 100;
    }

    public void createComplete() {
        this.op = Agree.Yes.getCode();
        this.opDate = new Date();
        this.progress = 100;
        this.objNewStatus = this.objOldStatus;
        this.caption = "创建";
    }

    public void createStart() {
        this.op = 0;
        this.opDate = null;
        this.progress = 0;
    }

    public String getObjType() {
        return objType;
    }

    public void setObjType(String objType) {
        this.objType = objType;
    }

    public String getObjPropType() {
        return objPropType;
    }

    public void setObjPropType(String objPropType) {
        this.objPropType = objPropType;
    }

    public Long getObjId() {
        return objId;
    }

    public void setObjId(Long objId) {
        this.objId = objId;
    }

    public String getObjUuid() {
        return objUuid;
    }

    public void setObjUuid(String objUuid) {
        this.objUuid = objUuid;
    }

    public Long getTwfId() {
        return twfId;
    }

    public void setTwfId(Long twfId) {
        this.twfId = twfId;
    }

    public String getTwfUuid() {
        return twfUuid;
    }

    public void setTwfUuid(String twfUuid) {
        this.twfUuid = twfUuid;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public int getOp() {
        return op;
    }

    public void setOp(int op) {
        this.op = op;
    }

    public Date getOpDate() {
        return opDate;
    }

    public void setOpDate(Date opDate) {
        this.opDate = opDate;
    }

    public Long getCuserId() {
        return cuserId;
    }

    public void setCuserId(Long cuserId) {
        this.cuserId = cuserId;
    }

    public String getCuserName() {
        return cuserName;
    }

    public void setCuserName(String cuserName) {
        this.cuserName = cuserName;
    }

    public String getCuserPhone() {
        return cuserPhone;
    }

    public void setCuserPhone(String cuserPhone) {
        this.cuserPhone = cuserPhone;
    }

    public String getCremark() {
        return cremark;
    }

    public void setCremark(String cremark) {
        this.cremark = cremark;
    }

    public boolean isAutoCreateStartNode() {
        return autoCreateStartNode;
    }

    public void setAutoCreateStartNode(boolean autoCreateStartNode) {
        this.autoCreateStartNode = autoCreateStartNode;
    }

    public int getFlowType() {
        return flowType;
    }

    public void setFlowType(int flowType) {
        this.flowType = flowType;
    }

    public Integer getObjOldStatus() {
        return objOldStatus;
    }

    public void setObjOldStatus(Integer objOldStatus) {
        this.objOldStatus = objOldStatus;
        this.objOldStatusCaption = Status.get(objOldStatus) != null ? Status.get(objOldStatus).getMessage() : "";
    }

    public Integer getObjNewStatus() {
        return objNewStatus;
    }

    public void setObjNewStatus(Integer objNewStatus) {
        this.objNewStatus = objNewStatus;
        this.objNewStatusCaption = Status.get(objNewStatus) != null ? Status.get(objNewStatus).getMessage() : "";
    }

    public String getObjOldStatusCaption() {
        return objOldStatusCaption;
    }

    public void setObjOldStatusCaption(String objOldStatusCaption) {
        this.objOldStatusCaption = objOldStatusCaption;
    }

    public String getObjNewStatusCaption() {
        return objNewStatusCaption;
    }

    public void setObjNewStatusCaption(String objNewStatusCaption) {
        this.objNewStatusCaption = objNewStatusCaption;
    }

    public String getObjCompareJson() {
        return objCompareJson;
    }

    public void setObjCompareJson(String objCompareJson) {
        this.objCompareJson = objCompareJson;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public List<CompareNameValue> convertObjCompare() {
        if (!Strings.isBlank(objCompareJson)) {
            return JSON.parseArray(objCompareJson, CompareNameValue.class);
        }
        return null;
    }

    public boolean isNeedTwfFlow() {
        return needTwfFlow;
    }

    public void setNeedTwfFlow(boolean needTwfFlow) {
        this.needTwfFlow = needTwfFlow;
    }

    public void setTwfArgs(Domain entity) {
        if (!Strings.isBlank(entity.getTwfDefineCode())) {
            this.setNeedTwfFlow(true);
            this.setCompleteThenDealBz(false);
            // 流程定义code
            this.setTwfDefineCode(entity.getTwfDefineCode());
            // 业务自定义字段//String groupName, String fieldName, String fieldValue
            if (entity.getTwfBaseInfo() != null && !entity.getTwfBaseInfo().isEmpty()) {
                this.setTwfBaseInfo(entity.getTwfBaseInfo());
            }
            // 流程图参数字段
            if (entity.getTwfChartArgs() != null && !entity.getTwfChartArgs().isEmpty()) {
                this.setTwfChartArgs(entity.getTwfChartArgs());
            }
        } else {
            this.setNeedTwfFlow(false);
        }
    }

    public Boolean isCompleteThenDealBz() {
        return completeThenDealBz;
    }

    public void setCompleteThenDealBz(Boolean completeThenDealBz) {
        this.completeThenDealBz = completeThenDealBz;
    }

}
